package model;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;

import util.Message;


public class Main {

 static final int numFeet = 2;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		State state = new State(numFeet);
		LinkedBlockingQueue<Message> pipe = new LinkedBlockingQueue<Message>();
		
		Semaphore[] steps = new Semaphore[numFeet];
		CyclicBarrier[] barriers = new CyclicBarrier[numFeet];
		Foot[] foots = new Foot[numFeet];
		
		for(int id = 0; id < numFeet; id++) {
			steps[id] = new Semaphore(1);
			barriers[id] = new CyclicBarrier(2);
			foots[id] = new Foot(id, "./env" + (id + 1), state, pipe, steps[id], barriers[id]);
		}
		

		
		Head head = new Head(state, pipe, steps, barriers);

		
		ExecutorService exec = Executors.newCachedThreadPool();
		for(int i = 0; i < numFeet; i++) {
			exec.execute(foots[i]);
		}
		head.run();
	}

}
